package it.sd.wdc.spider.htmlbrowser;

import it.sd.utility.RegExpTokenizer2;



public class HttpUtility {

	public static HttpPageMeta getPageMeta(String url) throws WebClientException {
		
		
		WebTcpClientSession session =  WebTcpClientFactory.getInstance().newSession();
		
		String page = session.getPage(url);
		String head = extractHead(page);
		
		HttpPageMeta meta = extractMeta(head);
		
		return meta;
	}

	private static String extractHead(String page) {
		String head = null;
		
		String page2 = page.toUpperCase();
		int inizio = page2.indexOf("<HEAD");
		if (inizio>-1) {
			int fine = page2.indexOf("</HEAD>", inizio);
			if (fine>-1) {
				head = page.substring(inizio, fine);
			}
 		}
		
		return head;
	}
	
	public static void main(String[] args) {
		
		try {
			System.out.println(getPageMeta("www.fantascienza.com"));
			System.out.println(getPageMeta("http://it.wikipedia.org/wiki/Fantascienza"));
			System.out.println(getPageMeta("http://en.wikipedia.org/wiki/Science_fiction"));
			System.out.println(getPageMeta("http://www.imdb.com/title/tt1853728/"));
			System.out.println(getPageMeta("http://www.kubrick2001.com/"));
			System.out.println(getPageMeta("http://www.blade-runner.it/"));
			
		} catch (Exception e) {
			e.printStackTrace();
			
		}
	}
	
	private static HttpPageMeta extractMeta(String head) {
		HttpPageMeta meta = new HttpPageMeta();
		
		RegExpTokenizer2 re = new RegExpTokenizer2(head, "(?s)(?<=\\<title\\>|\\<TITLE\\>)(.*?)(?=\\<\\/title\\>|\\<\\/TITLE\\>)");
		if (re.hasNext()) {
			meta.setTitle(trimSpace(re.next()));
		}
		
		re = new RegExpTokenizer2(head, "(?s)(?<=\\<meta|\\<META)(.*?)(?=\\>)");
		while (re.hasNext()) {
			String m = re.next();
			RegExpTokenizer2 rex = new RegExpTokenizer2(m, "(?s)(?<=name\\=\\\"|NAME\\=\\\")(.*?)(?=\\\")");
			if (rex.hasNext()) {
				String name = rex.next();
				rex = new RegExpTokenizer2(m, "(?s)(?<=content\\=\\\"|CONTENT\\=\\\")(.*?)(?=\\\")");
				if (rex.hasNext()) {
					String content = rex.next();
					meta.put(name, content);
				}	
			}
		}
		
		
		return meta;
	}

	  public static String removeTag(String v) {
		  if (v==null) return v;
		  
		  StringBuffer o = new StringBuffer();
		  boolean intag = false;

		  for (int i = 0; i < v.length(); i++) {
			  char c =v.charAt(i);
			  if (intag) {
				  if (c=='>') {
					  intag = false;
				  }
			  } else {
				  if (c=='<') {
					  intag = true;
				  } else {
					  o.append(c);
				  }
			  }
		  
		  }
		  return o.toString();
	  }
	  
	  public static String trimSpace(String v) {
		  if (v==null) return v;
		  
		  StringBuffer o = new StringBuffer();
		  boolean space = false;
		  v = v.trim();

		  for (int i = 0; i < v.length(); i++) {
			  char c =v.charAt(i);
			  if ((c==' ')|| 
				  (c=='\t')||
			      (c=='\n')||
			      (c=='\r')) {
				  if (!space) {
					  space=true;
					  o.append(" ");
				  }
			  } else {
				  o.append(c);
				  space = false;
			  }
		  
		  }
		  
		  return o.toString();
	  }

}
